import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'home',
    component: HomeView
  },

  {
    path: '/branch',
    name: 'branch',
    component: () => import('../views/BranchView.vue')
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import('../views/ShopPing.vue'),
    beforeEnter: (to, from, next) => {
      console.log(to);
      console.log(from);
      let token = window.localStorage.getItem('token')
      if (!token) {
        next('/login');
        return
      } else {
        next()
      }
    }
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/MyView.vue'),
    beforeEnter: (to, from, next) => {
      console.log(to);
      console.log(from);
      let token = window.localStorage.getItem('token')
      if (!token) {
        next('/login');
        return
      } else {
        next()
      }
    }
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView
  },
  {
    path: '/list',
    name: 'list',
    component: () => import('../views/ProductList.vue')
  },
  {
    path: '/alis',
    name: 'alis',
    component: () => import('../views/DetAils.vue')
  },
  {
    path: '/order',
    name: 'order',
    component: () => import('../views/OrderView.vue')
  },
  {
    path: '/ress',
    name: 'ress',
    component: () => import('../views/RessList.vue')
  },
  {
    path: '/search',
    name: 'search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path: '/searchClassic',
    name: 'searchClassic',
    component: () => import('../views/SearchClassic.vue')
  },
  {
    path: '/purch',
    name: 'purch',
    component: () => import('../views/PurchaseView.vue')
  },
  {
    path: '/barlist',
    name: 'barlist',
    component: () => import('../views/BranchList.vue')
  },
  {
    path: '/colle',
    name: 'colle',
    component: () => import('../views/CollectionView.vue')
  },
  {
    path: '/member',
    name: 'member',
    component: () => import('../views/MemberView.vue')
  },
]
// 注意:解决路由跳转问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
} 

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router